package com.sgb.kjwl.view.ui.activity.home;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtEnterpriseConfigBinding;
import com.sgb.kjwl.view.ui.adapter.FragPagerAdapter;
import com.sgb.kjwl.view.ui.fragment.home.DisplaySettingsConfigFragment;
import com.sgb.kjwl.view.ui.fragment.home.HomeCompanyConfigFragment;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.mmkvutil.CacheMarker;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.ResolutionUtil;

import java.util.ArrayList;

/**
 * 首页企业配置类
 * Create by LiWei  2021/11/2
 */
@Route(path = AppRouteConstant.EnterpriseConfigActivity)
public class EnterpriseConfigActivity extends AppActivity {

    private AtEnterpriseConfigBinding binding;
    private ArrayList<Fragment> mFragmentList;
    private String[] mTitles = new String[]{"进入网页", "展示设置"};
    public boolean isSettingChange = false;
    private String currentKey = "";

    private LinearLayout.LayoutParams layoutParams;
    private int tabWidth;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, EnterpriseConfigActivity.class);
        context.startActivity(intent);
    }

    public static void start(Context context, String key) {
        Intent intent = new Intent();
        intent.setClass(context, EnterpriseConfigActivity.class);
        intent.putExtra("key", key);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.at_enterprise_config);
        //设置Activity的弹出方式
        overridePendingTransition(R.anim.anim_activity_up, 0);
    }

    @Override
    protected void init() {
        binding.btnIvCloseWindow.setOnClickListener(this::onClick);
        binding.tvHandleBusiness.setOnClickListener(this::onClick);

        if (null != getIntent()) {
            currentKey = getIntent().getStringExtra("key");
        }

        mFragmentList = new ArrayList<>();
        mFragmentList.add(CorporateWebsiteDisplayFragment.newInstance(currentKey));
        mFragmentList.add(DisplaySettingsConfigFragment.newInstance());
        //首页企业配置界面
        //mFragmentList.add(CompanyConfigSortFragment.newInstance());

        initTabLayout(mTitles);
    }

    /**
     * 展示顶部标题菜单
     *
     * @param mTitles 标题列表
     */
    private void initTabLayout(String[] mTitles) {
        binding.tlSlidingTabLayout.clearOnTabSelectedListeners();
        binding.tlSlidingTabLayout.removeAllTabs();
        FragPagerAdapter pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), mFragmentList, mTitles);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.tlSlidingTabLayout.setTabMode(TabLayout.MODE_FIXED);
        binding.tlSlidingTabLayout.setupWithViewPager(binding.viewpager);
        binding.viewpager.setCurrentItem(0);
        for (int i = 0; i < mTitles.length; i++) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_enterprise_config_tab_menu, null, false);
            binding.tlSlidingTabLayout.getTabAt(i).setCustomView(view);
            TextView tv = binding.tlSlidingTabLayout.getTabAt(i).getCustomView().findViewById(R.id.tv_tabItem);
            tv.setText(mTitles[i]);
            if (i == 0) {
                tv.setTextSize(16);
                tv.setTypeface(Typeface.DEFAULT_BOLD);
            }
        }
        binding.tlSlidingTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView tv = tab.getCustomView().findViewById(R.id.tv_tabItem);
                tv.setTextSize(16);
                tv.setTypeface(Typeface.DEFAULT_BOLD);
                tv.invalidate();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView tv = tab.getCustomView().findViewById(R.id.tv_tabItem);
                tv.setTextSize(15);
                tv.setTypeface(Typeface.DEFAULT);
                tv.invalidate();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        layoutParams = (LinearLayout.LayoutParams) binding.indicationLine.getLayoutParams();
        tabWidth = (ResolutionUtil.getScreenWidth(getActivity()) - ResolutionUtil.dp2px(this, 40)) / 3;
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                layoutParams.leftMargin = (int) ((tabWidth - ResolutionUtil.dp2px(getActivity(), 20)) / 2 + positionOffset * tabWidth + position * tabWidth);
                binding.indicationLine.setLayoutParams(layoutParams);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.btn_tab1:
//                binding.btnTab1.setSelected(true);
//                binding.btnTab2.setSelected(false);
//                changeFragment(0);
//                break;
//            case R.id.btn_tab2:
//                binding.btnTab1.setSelected(false);
//                binding.btnTab2.setSelected(true);
//                changeFragment(1);
//                break;
            case R.id.btn_iv_close_window:
//                if (isSettingChange){
//                    showDialog();
//                }else{
//                    finish();
//                }
                if (binding.tlSlidingTabLayout.getSelectedTabPosition() == 1) {
                    showDialog();
                } else {
                    finish();
                }
                break;
            case R.id.tv_handle_business:
                MToast.showToast(this, "功能开发中");
                break;
        }
    }

    public void showDialog() {
        new RoundDialog.Builder(getActivity())
                .title("提示")
                .content("确定要退出吗？已更改的内容将不会保存!")
                .cancel("取消")
                .confirm("确认")
                .contentGravity(Gravity.CENTER)
                .cancelable(false)
                .confirmCallback(v -> {
                    finish();
                })
                .show();
    }

    @Override
    public void finish() {
        super.finish();
        // 结束动画,关闭的时候从顶部向下滑动消失
        this.overridePendingTransition(0, R.anim.anim_activity_close);
    }
}
